<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<script src="js/md5.js"></script>
<style>
    .control-label {
        margin: 10px;
        width: 80px;
    }

    .message {
        color: red;
    }
</style>
<script type="text/javascript">
    $(function () {

        $.CurrentDialog.find("#oldPassword").blur(function () {
            $.CurrentDialog.find("#oldPasswordMsg").html("");
            passwordAjax();
        });

        $.CurrentDialog.find(".control-label x180").each(function () {
            $(this).bind('input propertychange', function () {
                $(".msg").hidden();
            })
        })

    });

    var flag1 = false;
    //原始密码Ajax验证
    function passwordAjax() {
        var password = $.CurrentDialog.find("#oldPassword").val();
        password=hex_md5(password);
        $.ajax({
            type: "POST",
            url: "user/ajaxPassword.shtml",
            dataType: "json",
            data: {
                password: password
            },
            success: function (json) {
                if (json.code == 0) {
                    $.CurrentDialog.find("#oldPasswordMsg").html(json.msg);
                    $.CurrentDialog.find("#oldPasswordMsg").css("color", "green");
                    $.CurrentDialog.find("#oldPassword").css('border', '1px green solid');
                    flag1 = true;
                } else {
                    $.CurrentDialog.find("#oldPasswordMsg").html(json.msg);
                    $.CurrentDialog.find("#oldPasswordMsg").css("color", "red");
                    $.CurrentDialog.find("#oldPassword").css('border', '1px red solid');
                    flag1 = false;
                }

            },
            error: function (json) {
                if (json.statusCode == 300 && json.message == "系统错误，操作失败！") {
                    $(this).alertmsg('error', '系统错误，操作失败！')
                }
            }
        });
    }
    ;

    function doAjaxCallBackForChangePasswordDialog(json) {
        //提交成功后关闭该弹窗并返回登录页面
        $(this).bjuiajax('ajaxDone', json);
        if (json.code == 0) {
            setTimeout(function () {
                wait()
            }, 2000);
            function wait() {
                Dracula.get("admin/login.shtml")
            }
        }
    }

    function check() {
        if (!flag1) {
            $(this).alertmsg('warn', '原始密码错误，请重新填写')
            return false;

        } else {
            //密码加密
            var pwd = $.CurrentDialog.find("#newPassword").val();
            var passd = hex_md5(pwd);
            $.CurrentDialog.find("#realPassword").val(passd);
            return true;
        }
    }
    ;
</script>
<form action="user/changePasswordSubmit.shtml" class="pageForm"
      data-toggle="validate"
      data-callback="doAjaxCallBackForChangePasswordDialog" method="post"
      id="pageForm" data-toggle="ajaxform"
      data-validator-option="{stopOnError:false, timely:2}">
    <div class="bjui-pageContent">
        <div class="panel panel-default">
            <div class="panel-body">
                <table class="table table-condensed table-hover">
                    <tbody>
                    <tr>
                        <td><label class="control-label x180">用户名称：</label></td>
                        <td>${sessionScope.user_session_key.name }</td>
                    </tr>
                    <tr>
                        <td><label for="oldPassword" class="control-label x180">原始密码：</label></td>
                        <td><input type="password" name="oldPassword" class="in"
                                   id="oldPassword"><span id="oldPasswordMsg"
                                                          class="message">*</span></td>
                    </tr>
                    <tr>
                        <td><label class="control-label x180">新密码：</label></td>
                        <td><input type="password" name="newPassword" class="in" id="newPassword"
                                   data-rule="密码: required;length[~128]"><span id="newPasswordMsg"
                                                                               class="message">*</span></td>
                    </tr>
                    <input type="hidden" name="realPassword" id="realPassword">
                    <tr>
                        <td><label for="newPassword2" class="control-label x180">确认密码：</label></td>
                        <td><input type="password" name="newPassword2" class="in" id="newPassword2"
                                   data-rule="确认密码: required;match(newPassword);"><span
                                id="newPassword2Msg" class="message">*</span></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="bjui-pageFooter">
        <ul>
            <li>
                <button type="submit" class="btn-primary"
                        onClick="return check()">保存
                </button>
            </li>
            <li>
                <button type="button" class="btn-close">关闭</button>
            </li>
        </ul>
    </div>
</form>